package com.medical.data.annotationImpl;

import cn.hutool.core.util.IdcardUtil;
import com.medical.data.annotations.IdCardValidate;
import com.medical.data.utils.ResetMessageUtil;
import org.springframework.util.ObjectUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

public class IdCardValidateImpl  implements ConstraintValidator<IdCardValidate, String> {
    @Override
    public void initialize(IdCardValidate constraintAnnotation) {
        ConstraintValidator.super.initialize(constraintAnnotation);
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        if (ObjectUtils.isEmpty(value)) {
            return false;
        }

        if (value.contains(" ")) {
            ResetMessageUtil.reset(context, "身份证号码，格式错误：不能包含空格");
            return false;
        }

        return IdcardUtil.isValidCard(value);
    }




}
